package com.charles.video.common.security.auth;

import com.charles.video.api.model.MemberLogin;
import com.charles.video.common.core.exception.NotLoginException;
import com.charles.video.common.core.utils.SpringUtils;
import com.charles.video.common.security.service.TokenService;
import com.charles.video.common.security.utils.SecurityUtils;

/**
 * @author Charles-H
 * 
 * 权限验证逻辑
 */
public class AuthLogic {
    
    public TokenService tokenService = SpringUtils.getBean(TokenService.class);

    /** 注销会话 */
    public void logout() {
        String token = SecurityUtils.getToken();
        if (token == null) return;
        logoutByToken(token);
    }
    
    /** 通过token注销会话 */
    public void logoutByToken(String token) {
        tokenService.deleteMemberLogin(token);
    }
    
    /** 判断成员是否登录 */
    public void checkLogin() {
        getMemberLogin();
    }
    
    /** 获取当前成员信息 */
    public MemberLogin getMemberLogin() {
        String token = SecurityUtils.getToken();
        if (token == null) throw new NotLoginException("未提供token令牌");
        MemberLogin memberLogin = SecurityUtils.getMemberLogin();
        if (memberLogin == null) throw new NotLoginException("token令牌无效");
        return memberLogin;
    }
    
    /** 通过token令牌获取成员信息 */
    public MemberLogin getMemberLogin(String token) {
        return tokenService.getMemberLogin(token);
    }
    
    /** 查看当前成员有效期，若不足120分钟，则自动刷新缓存 */
    public void verifyUserLoginExpire(MemberLogin memberLogin) {
        tokenService.verifyToken(memberLogin);
    }
    
}
